package cc.yugu.yhdw.webserver.web.controller.security.hdw;

import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.domain.hdw.ExchangeAmountMoneyDivideDetail;
import cc.yugu.yhdw.common.domain.hdw.WithdrawCash;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.common.utils.HttpClientUtils;
import cc.yugu.yhdw.common.utils.HttpUtils;
import cc.yugu.yhdw.common.utils.IdUtils;
import cc.yugu.yhdw.common.utils.YhdwUtils;
import cc.yugu.yhdw.webserver.constant.AppConstEnum;
import cc.yugu.yhdw.webserver.service.hdw.WithdrawCashService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping(value = "/security/hdw/withdraw_cash")
public class WithdrawCashController extends SecurityController {
    final static String PORTRAIT_URL = "/security/hdw_upload/withdraw_cash_image.htm";
    @Autowired
    WithdrawCashService withdrawCashService;

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping(value = "image.htm", method = RequestMethod.GET)
    public void image() {
    }

    @RequestMapping(value = "image.htm", method = RequestMethod.POST)
    public String image(@RequestParam("file") MultipartFile file, Model model) throws IOException {
        File sourceFile = new File(getAppConfig().tempDir, IdUtils.uuid() + "." + YhdwUtils.getFileSuffix(file.getOriginalFilename()));
        YhdwUtils.makeParentDir(sourceFile);
        file.transferTo(sourceFile);
        HttpUtils.HttpResp httpResp = HttpUtils.uploadFile(getAppConfig().staticUrl + PORTRAIT_URL, sourceFile, Collections.EMPTY_MAP); //upload to static server
        if (httpResp.status / 100 == 2) {
            Map map = (Map) YhdwUtils.decodeJson(httpResp.content, Map.class);
            Map data = (Map) map.get("data");

            model.addAttribute("success", true);
            model.addAttribute("filePath", data.get("filePath"));
            model.addAttribute("fileName", data.get("fileName"));
        } else {
            model.addAttribute("success", false);
            model.addAttribute("message", "上传文件出现错误");
        }

        return "/security/hdw/withdraw_cash/image_response";
    }

    @RequestMapping(value = "index.htm")
    public void index(Model model) {
        ExchangeAmountMoneyDivideDetail.OrgType[] orgTypes = ExchangeAmountMoneyDivideDetail.OrgType.values();
        List<ExchangeAmountMoneyDivideDetail.OrgType> orgTypeList = new ArrayList<ExchangeAmountMoneyDivideDetail.OrgType>();
        for (ExchangeAmountMoneyDivideDetail.OrgType orgType : orgTypes) {
            if (orgType.getValue() != ExchangeAmountMoneyDivideDetail.OrgType.HQ.getValue()) {
                orgTypeList.add(orgType);
            }
        }
        model.addAttribute("orgTypeList", orgTypeList);
//        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_02_03_09.getValue());
    }

    @RequestMapping(value = "page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(WithdrawCash withdrawCash) {
        return PageResult.successResult(withdrawCashService.findPage(withdrawCash));
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping("add.htm")
    public void add(Model model) {
        ExchangeAmountMoneyDivideDetail.OrgType[] orgTypes = ExchangeAmountMoneyDivideDetail.OrgType.values();
        List<ExchangeAmountMoneyDivideDetail.OrgType> orgTypeList = new ArrayList<ExchangeAmountMoneyDivideDetail.OrgType>();
        for (ExchangeAmountMoneyDivideDetail.OrgType orgType : orgTypes) {
            if (orgType.getValue() != ExchangeAmountMoneyDivideDetail.OrgType.HQ.getValue()) {
                orgTypeList.add(orgType);
            }
        }
        model.addAttribute("orgTypeList", orgTypeList);
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping(value = "edit.htm")
    public void edit(int id, Model model) {
        WithdrawCash entity = withdrawCashService.find(id);
        model.addAttribute("entity", entity);
        ExchangeAmountMoneyDivideDetail.OrgType[] orgTypes = ExchangeAmountMoneyDivideDetail.OrgType.values();
        List<ExchangeAmountMoneyDivideDetail.OrgType> orgTypeList = new ArrayList<ExchangeAmountMoneyDivideDetail.OrgType>();
        for (ExchangeAmountMoneyDivideDetail.OrgType orgType : orgTypes) {
            if (orgType.getValue() != ExchangeAmountMoneyDivideDetail.OrgType.HQ.getValue()) {
                orgTypeList.add(orgType);
            }
        }
        model.addAttribute("orgTypeList", orgTypeList);
    }

    @ResponseBody
    @ViewModel(ViewModel.JSON)
    @RequestMapping(value = "create.htm")
    public ExtResult create(WithdrawCash entity) {
        return withdrawCashService.insert(entity);
    }

    @ResponseBody
    @ViewModel(ViewModel.JSON)
    @RequestMapping(value = "update.htm")
    public ExtResult update(WithdrawCash entity) {
        return withdrawCashService.update(entity);
    }

    @RequestMapping(value = "view.htm")
    public String view(Model model, int id) {
        WithdrawCash entity = withdrawCashService.find(id);
        model.addAttribute("entity", entity);
        return "/security/hdw/withdraw_cash/view";
    }

    @RequestMapping("delete.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult delete(int id) {
        withdrawCashService.delete(id);
        return ExtResult.successResult();
    }


}
